<template>
  <el-dialog v-model="dialogTableVisible" title="个人信息" width="500px">
    <el-form style="background-color: #cccccc79 ;padding: 50px" ref="form" :model="form" size="small" :rules="rules">
      <el-form-item prop="username">
        <el-input placeholder="用户名" prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item prop="sex">
        <el-radio-group v-model="form.sex" class="ml-4">

          <el-radio label="男" size="large">男</el-radio>
          <el-radio label="女" size="large">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="age">
        <el-input placeholder="年龄" prefix-icon="el-icon-user-solid" v-model="form.age"></el-input>
      </el-form-item>
      <el-form-item prop="jobTitle">
        <el-select v-model="form.jobTitle"  placeholder="职位" >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input placeholder="手机号码" prefix-icon="el-icon-user-solid" v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input placeholder="请设置密码" prefix-icon="el-icon-lock" v-model="form.password" show-password></el-input>
      </el-form-item>
      <el-form-item prop="confirm">
        <el-input placeholder="请确认密码" prefix-icon="el-icon-lock" v-model="form.confirm" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="width: 100%" type="primary" @click="update">更 新</el-button>
      </el-form-item>
      <el-button style="width: 100%" type="danger" @click="escape()">退出登录</el-button>
    </el-form>
  </el-dialog>
</template>

<script>
//
import request from "@/utils/request";
// import { ElMessageBox ,ElMessage} from 'element-plus'
export default {

  name: "UserInfoDialog",
  mounted() {
    this.form = JSON.parse(sessionStorage.getItem("user"));
    console.log(this.form);
  },
  data(){
    return {
      store:this.$store,
      form:sessionStorage.getItem("user"),
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        sex:[
          {required: true,message: '请选择性别',trigger: 'change'}
        ],
        jobTitle:[
          {required:true,message:'请选择职位',trigger:'blur'}
        ],
        age:[
          {required: true, message: '请输入年龄', trigger: 'blur'},
        ],
        phone:[
          {required: true, message: '请输入手机号码', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        confirm: [
          {required: true, message: '请确认密码', trigger: 'blur'},
        ],
      },
      options : [
        {
          value: 'Option1',
          label: 'Option1',
        },
        {
          value: 'Option2',
          label: 'Option2',
        },
        {
          value: 'Option3',
          label: 'Option3',
        },
        {
          value: 'Option4',
          label: 'Option4',
        },
        {
          value: 'Option5',
          label: 'Option5',
        },
      ]
    }
  },
  computed:{
    dialogTableVisible:{
      get (){
        return this.store.state.userInfoDialogVisible;
      },
      set(userInfoDialogVisible){
        this.store.commit('setUserInfoDialogVisible',userInfoDialogVisible);
      }
    },
  },
  methods:{
    escape:function (){
      window.sessionStorage.clear();
      window.localStorage.clear();
      window.location.reload();
      this.$router.replace("/index");
    },
    update:function (){
      if (this.form.password !== this.form.confirm) {
        this.$message({
          type: "error",
          message: '2次密码输入不一致！'
        })
        return
      }

      this.$refs['form'].validate((valid) => {
        if (valid) {
          // this.form.permission=2;
          request.post("/user/update", this.form).then(res => {
            //console.log(res);
            if (res.data.code === 0) {
              this.$message({
                type: "success",
                message: "更新成功"
              })
              this.store.commit('setUserInfoDialogVisible',false);
            } else {
              this.$message({
                type: "error",
                message: res.data.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>